package firbi.base.util;

import android.location.Location;

import com.google.android.maps.GeoPoint;

import firbi.base.com.BusStop;

/**
 * This class provides helper methods used by the application. 
 *
 */
public class FirbiHelper {
	protected FirbiHelper() {
	}
	
	
	/**
     * Converts the lat, lon coordinates of a Location object to a GeoPoint object
     * @param loc: Location 
     * @return: GeoPoint representation of the coordinates
     */
   public static GeoPoint locToGeo (Location loc) {
    	return new GeoPoint( (int) (loc.getLatitude()*1E6), (int)(loc.getLongitude()*1E6));
    }
   
   /** 
    * Converts a BusStop's direction field to a String.
    * 
    * @param direction: Direction field of a BusStop
    * @return: String representation of that direction or
    * 			'--' if the direction is not defined. 
    */
   public static String directionToString(Integer direction) {
   	switch (direction) {
   		case BusStop.DIRECTION_E: return "E";
	    	case BusStop.DIRECTION_N: return "N";
	    	case BusStop.DIRECTION_NE: return "NE";
	    	case BusStop.DIRECTION_NW: return "NW";
	    	case BusStop.DIRECTION_S: return "S";
	    	case BusStop.DIRECTION_SE: return "S";
	    	case BusStop.DIRECTION_SW: return "SW";
	    	case BusStop.DIRECTION_W: return "W";
   		default: return "--";
   	}
   }
   
   
	/** 
	 * Returns the distance in feet or meters between two GeoPoints
	 * 
	 * @param p1: Point 1
	 * @param p2: Point 2
	 * @param meters: Whether to return distance in meters or feet
	 * @return: distance between two GeoPoints
	 */
	public static double getDistance(GeoPoint p1, GeoPoint p2, boolean meters) {
		double p1Long = (p1.getLongitudeE6()/1E6)/57.29577951;
		double p1Lat = (p1.getLatitudeE6()/1E6)/57.29577951;
		double p2Long = (p2.getLongitudeE6()/1E6)/57.29577951;
		double p2Lat = (p2.getLatitudeE6()/1E6)/57.29577951;
		
		double d = Math.acos(Math.sin(p1Lat)*Math.sin(p2Lat)+
				 Math.cos(p1Lat)*Math.cos(p2Lat)*Math.cos(p2Long-p1Long));
		if (meters) d *= 6371*1000;
		else  d *= 3963 * 5280; 
		
		return ((int)(d*100))/100.0;
	}
   
    
    /**
     * Checks whether the two GeoPoints are the same location
     * @param p1: First point to be checked against 
     * @param p2: Second point to be checked against
     * @return: True is they are the same
     */
   	public static boolean sameLocation (GeoPoint p1, GeoPoint p2) {
    	if (p1==null || p2==null)
    		return false;
    	float [] distance = new float[1];
 		Location.distanceBetween(p1.getLatitudeE6()/1E6d, p1.getLongitudeE6()/1E6d, 
 		    	p2.getLatitudeE6()/1E6d, p2.getLongitudeE6()/1E6d, distance);
 		return Math.abs(distance[0]) < Constants.sLOCATION_BUFFER;
    }
    
   
}